package com.analysis.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 日志工具类
 * 提供统一的日志记录方法和性能监控
 */
public class LogUtils {
    
    /**
     * 获取Logger实例
     */
    public static Logger getLogger(Class<?> clazz) {
        return LoggerFactory.getLogger(clazz);
    }
    
    /**
     * 获取Logger实例
     */
    public static Logger getLogger(String name) {
        return LoggerFactory.getLogger(name);
    }
    
    /**
     * 记录方法进入日志
     */
    public static void enterMethod(Logger logger, String methodName, Object... params) {
        if (logger.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder();
            sb.append("进入方法: ").append(methodName);
            if (params != null && params.length > 0) {
                sb.append(" 参数: ");
                for (int i = 0; i < params.length; i += 2) {
                    if (i + 1 < params.length) {
                        sb.append(params[i]).append("=").append(params[i + 1]);
                    } else {
                        sb.append(params[i]);
                    }
                    if (i + 2 < params.length) {
                        sb.append(", ");
                    }
                }
            }
            logger.debug(sb.toString());
        }
    }
    
    /**
     * 记录方法退出日志
     */
    public static void exitMethod(Logger logger, String methodName, Object result) {
        if (logger.isDebugEnabled()) {
            logger.debug("退出方法: {} 返回值: {}", methodName, result);
        }
    }
    
    /**
     * 记录方法执行时间
     */
    public static void logExecutionTime(Logger logger, String methodName, long startTime) {
        long executionTime = System.currentTimeMillis() - startTime;
        logger.info("方法 {} 执行时间: {}ms", methodName, executionTime);
    }
    
    /**
     * 记录AI模型调用日志
     */
    public static void logAiModelCall(Logger logger, String modelName, String question, long startTime, boolean success, String errorMsg) {
        long executionTime = System.currentTimeMillis() - startTime;
        if (success) {
            logger.info("AI模型调用成功 - 模型: {}, 问题: {}, 耗时: {}ms", modelName, question, executionTime);
        } else {
            logger.error("AI模型调用失败 - 模型: {}, 问题: {}, 耗时: {}ms, 错误: {}", modelName, question, executionTime, errorMsg);
        }
    }
    
    /**
     * 记录业务操作日志
     */
    public static void logBusinessOperation(Logger logger, String operation, String userId, Object... details) {
        StringBuilder sb = new StringBuilder();
        sb.append("业务操作: ").append(operation);
        if (userId != null) {
            sb.append(" 用户: ").append(userId);
        }
        if (details != null && details.length > 0) {
            sb.append(" 详情: ");
            for (Object detail : details) {
                sb.append(detail).append(" ");
            }
        }
        logger.info(sb.toString());
    }
    
    /**
     * 记录性能监控日志
     */
    public static void logPerformance(Logger logger, String operation, long startTime, Object... metrics) {
        long executionTime = System.currentTimeMillis() - startTime;
        StringBuilder sb = new StringBuilder();
        sb.append("性能监控 - 操作: ").append(operation).append(" 耗时: ").append(executionTime).append("ms");
        if (metrics != null && metrics.length > 0) {
            sb.append(" 指标: ");
            for (int i = 0; i < metrics.length; i += 2) {
                if (i + 1 < metrics.length) {
                    sb.append(metrics[i]).append("=").append(metrics[i + 1]);
                } else {
                    sb.append(metrics[i]);
                }
                if (i + 2 < metrics.length) {
                    sb.append(", ");
                }
            }
        }
        logger.info(sb.toString());
    }
    
    /**
     * 记录异常日志
     */
    public static void logException(Logger logger, String message, Throwable throwable) {
        logger.error(message, throwable);
    }
    
    /**
     * 记录异常日志（无堆栈）
     */
    public static void logException(Logger logger, String message, String errorMsg) {
        logger.error("{} - 错误: {}", message, errorMsg);
    }
    
    /**
     * 记录信息日志
     */
    public static void logInfo(Logger logger, String message, Object... params) {
        if (params != null && params.length > 0) {
            StringBuilder sb = new StringBuilder();
            sb.append(message);
            for (int i = 0; i < params.length; i += 2) {
                if (i + 1 < params.length) {
                    sb.append(" ").append(params[i]).append("=").append(params[i + 1]);
                } else {
                    sb.append(" ").append(params[i]);
                }
            }
            logger.info(sb.toString());
        } else {
            logger.info(message);
        }
    }
    
    /**
     * 记录错误日志
     */
    public static void logError(Logger logger, String message, Object... params) {
        if (params != null && params.length > 0) {
            StringBuilder sb = new StringBuilder();
            sb.append(message);
            for (int i = 0; i < params.length; i += 2) {
                if (i + 1 < params.length) {
                    sb.append(" ").append(params[i]).append("=").append(params[i + 1]);
                } else {
                    sb.append(" ").append(params[i]);
                }
            }
            logger.error(sb.toString());
        } else {
            logger.error(message);
        }
    }
    
    /**
     * 记录错误日志（带异常）
     */
    public static void logError(Logger logger, String message, Throwable throwable) {
        logger.error(message, throwable);
    }
}
